<template>
  <div class="student-edit-container">
    <el-card>
      <div slot="header">
        <span>{{ isEdit ? '编辑学生' : '添加学生' }}</span>
      </div>

      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="form.studentNo"></el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker
              v-model="form.birthDate"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="班级" prop="className">
          <el-input v-model="form.className"></el-input>
        </el-form-item>

        <el-form-item label="专业" prop="major">
          <el-input v-model="form.major"></el-input>
        </el-form-item>

        <el-form-item label="学院" prop="college">
          <el-input v-model="form.college"></el-input>
        </el-form-item>

        <el-form-item label="入学日期" prop="admissionDate">
          <el-date-picker
              v-model="form.admissionDate"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { addStudent, updateStudent, getStudentDetail } from '@/api/student'

export default {
  name: 'StudentEdit',
  data() {
    return {
      isEdit: false,
      form: {
        studentNo: '',
        name: '',
        gender: '男',
        birthDate: '',
        className: '',
        major: '',
        college: '',
        admissionDate: '',
        address: '',
        phone: '',
        email: ''
      },
      rules: {
        studentNo: [
          { required: true, message: '请输入学号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        className: [
          { required: true, message: '请输入班级', trigger: 'blur' }
        ],
        major: [
          { required: true, message: '请输入专业', trigger: 'blur' }
        ],
        college: [
          { required: true, message: '请输入学院', trigger: 'blur' }
        ],
        admissionDate: [
          { required: true, message: '请选择入学日期', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    if (this.$route.params.id) {
      this.isEdit = true
      this.getStudentDetail(this.$route.params.id)
    }
  },
  methods: {
    getStudentDetail(id) {
      getStudentDetail(id).then(response => {
        this.form = response.data
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            updateStudent(this.$route.params.id, this.form).then(() => {
              this.$message.success('更新成功')
              this.$router.push('/student/list')
            })
          } else {
            addStudent(this.form).then(() => {
              this.$message.success('添加成功')
              this.$router.push('/student/list')
            })
          }
        }
      })
    },
    cancel() {
      this.$router.push('/student/list')
    }
  }
}
</script>

<style lang="scss" scoped>
.student-edit-container {
  padding: 20px;

  .el-form {
    width: 600px;
  }
}
</style>